// pages/mall/shoppingCar/shoppingCar.js
import * as Api_Mall_Cart from '../../../api/mall/shopCart'
import * as API_Mall_Coupons from '../../../api/mall/coupons'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    carList:[],
    lapseArr:[],
    finashed:true,
    totalPrice:0,
    params:{
      pageNum:1,
      pageSize:10
    },
    bottomBar:false,
    all:'N',
    cartNum:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initGlobalData();
    this.loadData()
    this.loadDataLapseList()
    this.loadCartNum()
  },
  loadCartNum(){
    Api_Mall_Cart.getCount().then(res=>{
      this.setData({
        cartNum:res.data
      })
    })
  },
  manager(){
    let bool = this.data.bottomBar
    this.setData({
      bottomBar:!bool
    })
  },
  exitManager(){
    let bool = this.data.bottomBar
    this.setData({
      bottomBar:!bool
    })
    this.calcPrice()
  },
  goConfirm(){
    let carList = this.data.carList.filter(item => item.select == "Y")
    if(carList.length == 0)
    return
    let res = carList.map((item,index) => {
      return Object.assign({},{'specsId':item.specsId,'countNum':item.count,'productId':item.productId})
    })
    wx.navigateTo({
      url: '../../mall/orderConfirm/orderConfirm?params='+JSON.stringify(res),
    })
  },
  

  selectAll(){
    if(this.data.carList.length == 0){
      return
    }
    let arr = this.data.carList.map(item => item.select)
    let YArr = []
    let NArr = []
    for (let i = 0; i < arr.length; i++) {
      const element = arr[i];
      if(element == "Y"){
        YArr.push(element)
      }else{
        NArr.push(element)
      }
    }
    if(YArr.length == this.data.carList.length){
      this.setFalse()
    }else{
      this.setTrue()
    }
     
  },
  setFalse(){
    let tempArr = this.data.carList
    for(let item of tempArr){
      item.select = "N"
    }
    this.setData({
      carList:tempArr,
      all:"N"
    })
    this.calcPrice()
  },
  setTrue(){
    let tempArr = this.data.carList
    for(let item of tempArr){
      item.select = "Y"
    }
    this.setData({
      carList:tempArr,
      all:"Y"
    })
    this.calcPrice()
  },
  checkSelect(){
    let arr = this.data.carList.map(item => item.select)
    let YArr = []
    let NArr = []
    for (let i = 0; i < arr.length; i++) {
      const element = arr[i];
      if(element == "Y"){
        YArr.push(element)
      }else{
        NArr.push(element)
      }
    }
    if(YArr.length == arr.length&&arr.length>0){
      this.setData({
        all:'Y'
      })
    }else{
      this.setData({
        all:'N'
      })
    }
    this.calcPrice()
  },
  selectItem(e){
    let item = this.data.carList[e.currentTarget.dataset.index]
    if(item.select == 'Y'){
      item.select = 'N'
    }else if(item.select == 'N'){
      item.select = 'Y'
    }
    let arr = this.data.carList
    arr[e.currentTarget.dataset.index] = item
    this.setData({
      carList:arr
    })
    
    this.checkSelect()
  },
  clear(){
    let arr = this.data.lapseArr.map(item => item.cartId)
    let params = {
      ids : arr.join(",")
    }
    Api_Mall_Cart.clear(params).then(res=>{
      this.loadDataLapseList()
    })
  },
  del(){
    let carList = this.data.carList.filter(item => item.select == "Y")
    if(carList.length==0){
      return
    }
    let ids = carList.map(item=>item.cartId)
    let params = {
      ids : ids.join(",")
    }
    Api_Mall_Cart.clear(params).then(res=>{
      this.setData({
        'params.pageNum':1,
        'pageSize':10,
        carList:[]
      })
        this.loadData()
       this.loadCartNum()
        this.checkSelect();
    })
  },
  loadDataLapseList(){
    Api_Mall_Cart.getLapseList().then(res=>{
      let arr = []
      for(var i = 0; i<res.data.length;i++){
        let item = res.data[i]
        for(var key in item){
          item[key].select = 'N'
          arr.push(item[key])
        }
      }
      this.setData({
        lapseArr:arr
      })
    })
  },
  loadData(){
    Api_Mall_Cart.getList(this.data.params).then(res=>{
      let arr = this.data.carList
      let bool = true
      for(var i = 0; i<res.data.length;i++){
        let item = res.data[i]
        for(var key in item){
          item[key].select = 'N'
          arr.push(item[key])
        }
        if(i == 0)
          bool = res.data[i].finished
      }
      /*let lapseArr = []
      let normalArr = []
      for(let item of arr){
        if(item.lapse){
          lapseArr.push(item)
        }else{
          normalArr.push(item)
        }
      }*/
      if(arr.length==0){
        this.setData({
          bottomBar:false
          
        }) 
      }
      this.setData({
        carList:arr,
        finashed:bool
      })
    })
  },
  initGlobalData:function(){
    let globalData=getApp().globalData;
    let statusBarHeightpx=globalData.statusBarHeightpx;
    let navHeadHeightpx=globalData.navHeadHeightpx;
    let navHeightpx=globalData.navHeightpx;
    this.setData({navHeadHeightpx:navHeadHeightpx,navHeightpx:navHeightpx,statusBarHeightpx:statusBarHeightpx});
  },
  resetNum(e){
    let that = this
    let select = this.data.carList[e.currentTarget.dataset.index].select
    if(e.currentTarget.dataset.num == 0){
      wx.showModal({
        title:"删除",
        content:"是否删除该商品",
        success(res){
          if(res.confirm){
            let params = {
              ids:e.currentTarget.dataset.id
            }
            Api_Mall_Cart.clear(params).then(res=>{
              if(res.code == 200){
                let arr = that.data.carList
                arr.splice(e.currentTarget.dataset.index,1)
                that.setData({
                  carList:arr
                })
                that.calcPrice()
                that.loadCartNum()

              }
            })
          }
        }
      })
      
      return
    }
    let params = {
      num:e.currentTarget.dataset.num,
      id:e.currentTarget.dataset.id
    }
    Api_Mall_Cart.update(params).then(ref=>{
      if(ref.code == 200){
        Api_Mall_Cart.flushSingle(e.currentTarget.dataset.id).then(res=>{
          let arr = this.data.carList
          res.data.select = select
          arr[e.currentTarget.dataset.index] = res.data
          this.setData({
            carList:arr
          })
          this.calcPrice()
          this.loadCartNum()
        })
      }
    })
  },
  calcPrice(){
    let oArr = []
    for (let i = 0; i < this.data.carList.length; i++) {
      const element = this.data.carList[i];
      if(element.select == 'Y'){
        oArr.push(element)
      }
    }
    if(oArr.length==0) {
      this.setData({
        totalPrice: 0
      })
      return
    }
    let arr = oArr.map(item => item.price*item.count)
    let res = arr.reduce((pre,cur)=>pre+cur);
    res = res.toFixed(2);
    this.setData({
      totalPrice: res
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.finashed){
      let page = this.data.params.pageNum + 1
      this.setData({
        'params.pageNum':page
      })
      this.loadData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})